package zhihuilan.common;


import org.springframework.util.ObjectUtils;

public class WxMiniResponse<T> {

    public static WxMiniResponse<?> EMPTY = WxMiniResponse.ok();

    private int code = 0;
    private T msg;
    private String message = "ok";

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getMsg() {
        if(!ObjectUtils.isEmpty(msg)){
            return msg;
        }else{
            return (T) message;
        }
    }

    public void setMsg(T msg) {
        this.msg = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public WxMiniResponse() {
    }

    public WxMiniResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public WxMiniResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.msg=data;
    }

    public static WxMiniResponse<?> ok() {
        return new WxMiniResponse<>();
    }

    public static <T> WxMiniResponse<T> ok(T body) {
        return new WxMiniResponse<T>(0, "", body);
    }

    public static <T> WxMiniResponse<T> ok(String message, T body) {
        return new WxMiniResponse<T>(0, message, body);
    }

    public static <T> WxMiniResponse<T> error(int errorCode, String message) {
        return new WxMiniResponse<T>(errorCode, message);
    }

    public static <T> WxMiniResponse<T> error(int errorCode, String message, T body) {
        return new WxMiniResponse<T>(errorCode, message, body);
    }

    public Boolean hasError() {
        return this.code != 0;
    }

}